package com.ruoyi.system.service.impl;

import java.util.List;

import com.ruoyi.common.core.domain.entity.TBProductType;
import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.bean.BeanValidators;
import com.ruoyi.system.domain.TBCustomer;
import com.ruoyi.system.mapper.TBProductTypeMapper;
import io.netty.util.internal.StringUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.system.mapper.TBDeviceMapper;
import com.ruoyi.system.domain.TBDevice;
import com.ruoyi.system.service.ITBDeviceService;

import javax.validation.Validator;

/**
 * 设备管理Service业务层处理
 * 
 * @author pspyy
 * @date 2022-03-07
 */
@Service
public class TBDeviceServiceImpl implements ITBDeviceService 
{

    private static final Logger log = LoggerFactory.getLogger(TBDeviceServiceImpl.class);
    @Autowired
    private TBDeviceMapper tBDeviceMapper;

    @Autowired
    protected Validator validator;

    @Autowired
    private TBProductTypeMapper tBProductTypeMapper;
    /**
     * 查询设备管理
     * 
     * @param id 设备管理主键
     * @return 设备管理
     */
    @Override
    public TBDevice selectTBDeviceById(Long id)
    {
        return tBDeviceMapper.selectTBDeviceById(id);
    }

    /**
     * 查询设备管理列表
     * 
     * @param tBDevice 设备管理
     * @return 设备管理
     */
    @Override
    public List<TBDevice> selectTBDeviceList(TBDevice tBDevice)
    {
        return tBDeviceMapper.selectTBDeviceList(tBDevice);
    }

    @Override
    public List<TBDevice> selectTBDeviceListById(TBDevice tBDevice) {
        return tBDeviceMapper.selectTBDeviceListById(tBDevice);
    }

    @Override
    public List<TBDevice> selectDeviceListByIds(String[] deviceIds) {
        return tBDeviceMapper.selectTBDeviceByDeviceIds(deviceIds);
    }

    /**
     * 新增设备管理
     * 
     * @param tBDevice 设备管理
     * @return 结果
     */
    @Override
    public int insertTBDevice(TBDevice tBDevice)
    {
        tBDevice.setIsSell("0");//0：未出售
        tBDevice.setCreateTime(DateUtils.getNowDate());
        return tBDeviceMapper.insertTBDevice(tBDevice);
    }

    /**
     * 修改设备管理
     * 
     * @param tBDevice 设备管理
     * @return 结果
     */
    @Override
    public int updateTBDevice(TBDevice tBDevice)
    {
        tBDevice.setUpdateTime(DateUtils.getNowDate());
        return tBDeviceMapper.updateTBDevice(tBDevice);
    }

    /**
     * 批量删除设备管理
     * 
     * @param ids 需要删除的设备管理主键
     * @return 结果
     */
    @Override
    public int deleteTBDeviceByIds(Long[] ids)
    {
        return tBDeviceMapper.deleteTBDeviceByIds(ids);
    }

    /**
     * 删除设备管理信息
     * 
     * @param id 设备管理主键
     * @return 结果
     */
    @Override
    public int deleteTBDeviceById(Long id)
    {
        return tBDeviceMapper.deleteTBDeviceById(id);
    }

    @Override
    public String checkDeviceNameUnique(String deviceName) {
        int i = tBDeviceMapper.checkDeviceNameUnique(deviceName);
        if(i  > 0){
            return "0";
        }
        return "1";
    }

    @Override
    public TBDevice selectDeviceByName(String deviceName) {
        return tBDeviceMapper.selectDeviceByName(deviceName);
    }

    @Override
    public String importCustomer(List<TBDevice> deviceList, Boolean isUpdateSupport, String operName) {
        if (StringUtils.isNull(deviceList) || deviceList.size() == 0)
        {
            throw new ServiceException("导入设备数据不能为空！");
        }
        int successNum = 0;
        int failureNum = 0;
        StringBuilder successMsg = new StringBuilder();
        StringBuilder failureMsg = new StringBuilder();
        for (TBDevice device : deviceList)
        {
            try
            {
                // 验证是否存在这个设备
                TBDevice dev = tBDeviceMapper.selectDeviceByName(device.getDeviceName());
                if (StringUtils.isNull(dev))
                {
                    TBProductType productType = tBProductTypeMapper.selectProductByName(device.getProductName());
                    if(StringUtils.isNotNull(productType)){
                        device.setProductTypeId(productType.getId());
                    }else {
                        failureNum++;
                        failureMsg.append("<br/>" + failureNum + "、产品类型 " + device.getProductName() + " 不存在");
                    }
                    BeanValidators.validateWithException(validator, device);
                    this.insertTBDevice(device);
                    successNum++;
                    successMsg.append("<br/>" + successNum + "、设备 " + device.getDeviceName() + " 导入成功");
                }
                else if (isUpdateSupport)
                {
                    BeanValidators.validateWithException(validator, device);
//                    this.updateTBDevice(device);
                    successNum++;
                    successMsg.append("<br/>" + successNum + "、设备 " + device.getDeviceName() + " 更新成功");
                }
                else
                {
                    failureNum++;
                    failureMsg.append("<br/>" + failureNum + "、设备 " + device.getDeviceName() + " 已存在");
                }
            }
            catch (Exception e)
            {
                failureNum++;
                String msg = "<br/>" + failureNum + "、设备 " + device.getDeviceName() + " 导入失败：";
                failureMsg.append(msg + e.getMessage());
                log.error(msg, e);
            }
        }
        if (failureNum > 0)
        {
            failureMsg.insert(0, "很抱歉，导入失败！共 " + failureNum + " 条数据格式不正确，错误如下：");
            throw new ServiceException(failureMsg.toString());
        }
        else
        {
            successMsg.insert(0, "恭喜您，数据已全部导入成功！共 " + successNum + " 条，数据如下：");
        }
        return successMsg.toString();
    }
}
